/* 
    记录每个请求的 url 和 ip

*/
const fs = require('fs')
const path = require('path')
const express = require('express')
const app = express()
const logger = require('./_unit/logs');

const filePath = path.resolve(__dirname, './test/04_全局中间件.txt')


let getAddressMiddleWare = (req, res, next) => {
    let { host, url } = req;
  
    let str = `主机：${host},  地址：${url}, 时间：${new Date()} \r\n`
    fs.appendFileSync(filePath, str, (err) => {
        if (err) return
        console.log("追加成功!");
    })
    next()
}


app.use(getAddressMiddleWare)

app.get('*', (req, res) => {
    let { host, url } = req;
    // logger.info(req.params, req.query, "参数")
    let str = `主机：${host},  地址：${url}, 时间：${new Date()} \r\n`
    res.send(`<h2>全局中间件： ${str}</h2>`)
})



app.listen(3000, () => {
    console.log('Server is running at http://localhost:3000')
})